﻿using System.IO;
using System.Xml;
using Microsoft.SharePoint.Utilities;

namespace SKN
{
    /// <summary>
    /// Contains common method for handling Out of Box Files 
    /// </summary>
    public static class OOTBFile
    {
        // Fields
        private static string _alertTemplateFilePath;
        private static string _docIconFilePath;
        // Properties

        // Fields
        private static string _themeFilePath;

        /// <summary>
        /// 
        /// </summary>
        public static string DocIconFilePath
        {
            get
            {
                if (_docIconFilePath == null)
                {
                    _docIconFilePath = Path.Combine(SPUtility.GetGenericSetupPath(@"TEMPLATE\XML"), "DOCICON.XML");
                }
                return _docIconFilePath;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string ThemeFilePath
        {
            get
            {
                if (_themeFilePath == null)
                {
                    _themeFilePath = Path.Combine(SPUtility.GetGenericSetupPath("TEMPLATE"),
                                                  @"LAYOUTS\1033\SPTHEMES.XML");
                }
                return _themeFilePath;
            }
        }

        // Fields

        /// <summary>
        /// 
        /// </summary>
        public static string AlertTemplateFilePath
        {
            get
            {
                if (_alertTemplateFilePath == null)
                {
                    _alertTemplateFilePath = Path.Combine(SPUtility.GetGenericSetupPath("TEMPLATE"),
                                                          @"XML\AlertTemplates.XML");
                }
                return _alertTemplateFilePath;
            }
        }

        // Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileExtension"></param>
        /// <param name="iconlocation"></param>
        public static void AddFileIcon(string fileExtension, string iconlocation)
        {
            var document = new XmlDocument();
            document.Load(DocIconFilePath);
            XmlNode documentElement = document.DocumentElement;
            if (null != documentElement)
            {
                if (documentElement.SelectSingleNode("ByExtension/Mapping[@Key='pdf']") == null)
                {
                    XmlNode newChild = document.CreateElement("Mapping");
                    newChild.Attributes.Append(document.CreateAttribute("Key")).Value = "pdf";
                    newChild.Attributes.Append(document.CreateAttribute("Value")).Value = "pdf.gif";
                    bool flag = false;
                    XmlNode xmlNode = documentElement.SelectSingleNode("ByExtension");
                    foreach (XmlNode child in xmlNode.ChildNodes)
                    {
                        if (child.Attributes["Key"].Value.CompareTo("pdf") > 0)
                        {
                            xmlNode.InsertBefore(newChild, child);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        xmlNode.AppendChild(newChild);
                    }
                    document.Save(DocIconFilePath);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileExtension"></param>
        public static void RemoveFileIcon(string fileExtension)
        {
            var document = new XmlDocument();
            document.Load(DocIconFilePath);
            XmlNode documentElement = document.DocumentElement;
            if (null != documentElement)
            {
                XmlNode oldChild = documentElement.SelectSingleNode("ByExtension/Mapping[@Key='pdf']");
                if (oldChild != null)
                {
                    documentElement.SelectSingleNode("ByExtension").RemoveChild(oldChild);
                    document.Save(DocIconFilePath);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="themeName"></param>
        public static void RegisterTheme(string themeName)
        {
            var document = new XmlDocument();
            document.Load(ThemeFilePath);
            XmlNode documentElement = document.DocumentElement;
            if (null != documentElement)
            {
                if (documentElement.SelectSingleNode("Templates[@TemplateID='" + themeName + "']") == null)
                {
                    XmlNode newChild = document.CreateElement("Templates");
                    newChild.Attributes.Append(document.CreateAttribute("TemplateID")).Value = themeName;
                    newChild.Attributes.Append(document.CreateAttribute("DisplayName")).Value = themeName;
                    newChild.Attributes.Append(document.CreateAttribute("Thumbnail")).Value = @"images/thnone.gif";
                    newChild.Attributes.Append(document.CreateAttribute("Preview")).Value = @"images/thnone.gif";
                    bool flag = false;
                    XmlNode xmlNode = documentElement.SelectSingleNode("/");

                    foreach (XmlNode child in xmlNode.ChildNodes)
                    {
                        if (child.Attributes["TemplateID"].Value.CompareTo("none") > 0)
                        {
                            xmlNode.InsertBefore(newChild, child);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        xmlNode.AppendChild(newChild);
                    }
                    document.Save(ThemeFilePath);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="themeName"></param>
        public static void DeregisterTheme(string themeName)
        {
            var document = new XmlDocument();
            document.Load(ThemeFilePath);
            XmlNode documentElement = document.DocumentElement;
            if (null != documentElement)
            {
                XmlNode oldChild = documentElement.SelectSingleNode("Templates[@TemplateID='" + themeName + "']");
                if (oldChild != null)
                {
                    documentElement.SelectSingleNode("/").RemoveChild(oldChild);
                    document.Save(ThemeFilePath);
                }
            }
        }
    }
}